clear all
set more off
global dirout ""
global dirin  ""
global dirobs ""

cd "$dirin"

use Puebla_weekly.dta, clear
duplicates drop
split week_date, p("-")
destring week_date1 week_date2 week_date3, replace
gen all_dates=mdy(week_date2, week_date3, week_date1)
gen temp_all_dates=all_dates
format %d all_dates
drop week_date*
rename client_uuid owner_uuid 
order owner_uuid all_dates, first
egen id=group(owner)
tsset id all_dates
egen new_user=tag(id)
save Puebla_weekly_introcash_analysis.dta, replace

use Puebla_weekly_introcash_analysis.dta, clear
collapse (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(all_dates)
egen total_trips=rowtotal(cash_trips epaid_trips)
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
drop if all_dates==21430
foreach var in cash_trips epaid_trips total_trips total_fares cash_fares_usd epaid_fares_usd {
replace `var'=`var'/1000000
} 

gen temp_total_fares=total_fares if all_dates==20877
gen temp_epaid_fares_usd=epaid_fares_usd if all_dates==20877
gen temp_cash_fares_usd=cash_fares_usd if all_dates==20877
egen max_total_fares=max(temp_total_fares)
egen max_epaid_fares_usd=max(temp_epaid_fares_usd)
egen max_cash_fares_usd=max(temp_cash_fares_usd)

capture drop *norm 
gen total_fares_norm=total_fares/max_total_fares
gen epaid_fares_usd_norm=epaid_fares_usd/max_total_fares
gen cash_fares_usd_norm=cash_fares_usd/max_total_fares

#delimit
twoway 
(line total_fares_norm all_dates, lwidth(thick) lcolor(black))  
(line epaid_fares_usd_norm all_dates, lwidth(thick) lcolor(gs6) lpattern(dash_dot))  
(line cash_fares_usd_norm all_dates, lwidth(thick) lcolor(gs12) lpattern(longdash_dot))  
, ytitle("Total fares (=1 at introduction of cash)", height(5)) 
 xtitle(" ") ylabel(0[1]2)
title(" ") xline(20877, lwidth(thin) lcolor(black) lpattern(dash))
xline(21157, lwidth(thin) lcolor(black) lpattern(dash))
legend(order (1 "Total" 2 "Fares card" 3 "Fares cash") cols(3) region(lwidth(none)))
graphregion(color(white)) plotregion(fcolor(white));
#delimit cr
graph export "$dirout/Figure7.eps", replace



cd ""

import excel "Updated San Luis.xlsx", sheet("Sheet1") firstrow clear
capture destring cash_fares_usd cash_miles, replace force
gen week=dofc(date)
format week %td
drop date
save "temp_update_sanluis.dta", replace

import excel "Viajes semanales Mexico para SLP", sheet("Sheet1") firstrow clear
capture destring cash_fares_usd cash_miles, replace force
*city_name date cash_trips epaid_trips cash_fares_usd epaid_fares_usd cash_miles epaid_miles
gen week=dofc(date)
format week %td
drop date
drop if week>=21738
append using "temp_update_sanluis.dta"
duplicates drop
tab week
erase "temp_update_sanluis.dta"

replace cash_fares_usd=0 if cash_fares_usd==.
replace cash_trips=0 if cash_trips==.
replace cash_miles=0 if cash_miles==.
egen total_trips=rowtotal(cash_trips epaid_trips)
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_miles=rowtotal(cash_miles epaid_miles)

foreach var in cash_trips epaid_trips cash_fares_usd epaid_fares_usd cash_miles epaid_miles total_trips total_fares total_miles {
rename `var' `var'NEW
}
egen city_id=group(city_name)
gen share_cash=cash_fares/total_fares
replace cash_trips=cash_trips/1000
replace epaid_trips=epaid_trips/1000
replace total_trips=total_trips/1000
replace cash_fares_usd= cash_fares_usd/1000
replace epaid_fares_usd = epaid_fares_usd/1000
replace total_fares=total_fares/1000


keep if city_name=="San Luis Potosi"
gen temp_total_fares=total_fares if week==21367
gen temp_epaid_fares_usd=epaid_fares_usd if week==21367
gen temp_cash_fares_usd=cash_fares_usd if week==21367
egen max_total_fares=max(temp_total_fares)
egen max_epaid_fares_usd=max(temp_epaid_fares_usd)
egen max_cash_fares_usd=max(temp_cash_fares_usd)

capture drop *norm 
gen total_fares_norm=total_fares/max_total_fares
gen epaid_fares_usd_norm=epaid_fares_usd/max_total_fares
gen cash_fares_usd_norm=cash_fares_usd/max_total_fares


#delimit
twoway 
(line total_fares_norm week if city_name=="San Luis Potosi" & week>=21367, lwidth(thick) lcolor(black))  
(line epaid_fares_usd_norm week if city_name=="San Luis Potosi" & week>=21367, lwidth(thick) lcolor(gs6) lpattern(dash_dot))    
(line cash_fares_usd_norm week if city_name=="San Luis Potosi" & week>=21367, lwidth(thick) lcolor(gs12) lpattern(longdash_dot))   
, ytitle("Total fares (=1 July 1, 2018)", height(5)) 
 xtitle(" ") ylabel(0[.25]1.25)
 xline(21738, lwidth(thin) lcolor(black) lpattern(dash))
legend(order (1 "Total" 2 "Fares card" 3 "Fares cash") cols(3) region(lwidth(none)))
graphregion(color(white)) plotregion(fcolor(white));
#delimit cr
graph export "$dirout/FigureG1.eps", replace


